import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import About from './views/Aboutperson.vue'
import Basket from './views/Basket.vue'
import Cake from './views/Cake.vue'
import Commercial from './views/Commercial.vue'
import Day from './views/Day.vue'
import Festival from './views/Festival.vue'
import Flower from './views/Flower.vue'
import Gift from './views/Gift.vue'
import Huazao from './views/Huazao.vue'
import Index from './views/Index.vue'
import Login from './views/Login.vue'
import Register from './views/Register.vue'
import Xiangxun from './views/Xiangxun.vue'
import Comment from './views/Comment.vue'
import Order from './views/Order'
import FlowerDetail from './views/FlowerDetail'
import OrtherDetail from './views/OrtherDetail'
import NotFound from './views/NotFound'


import Pay from "./components/Pay"
import Success from "./components/Success";
import Comfirm from "./components/Comfirm";
import CheckOrder from "./components/CheckOrder";
Vue.use(Router)
const originalPush=Router.prototype.push
Router.prototype.push=function push(location){
  return originalPush.call(this,location).catch(err=>err)
}

let router= new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      redirect:'/index'
    },
    {
      path: '/index',
      name: 'index',
      component:Index,
      // redirect:'/index/home',
      children:[
        {
          path:"home",
          name:"home",
          component:Home
        },
        {
          path:"about",
          name:"about",
          component:About
        },
        {
          path:"basket",
          name:"basket",
          component:Basket
        },
        {
          path:"cake",
          name:"cake",
          component:Cake
        },
        {
          path:"commercial",
          name:"commercial",
          component:Commercial
        },
        {
          path:"day",
          name:"day",
          component:Day
        },
        {
          path:"festival",
          name:"festival",
          component:Festival
        },
        {
          path:"flower",
          name:"flower",
          component:Flower
        },
        {
          path:"gift",
          name:"gift",
          component:Gift
        },
        {
          path:"huazao",
          name:"huazao",
          component:Huazao
        },
        {
          path:"xiangxun",
          name:"xiangxun",
          component:Xiangxun
        },
        {
          path:"detail",
          name:"detail",
          component:FlowerDetail
        },
        {
          path:"ortherDetail",
          name:"ortherDetail",
          component:OrtherDetail
        },
        {
          path:'comment',
          name:'comment',
          component:Comment
        },
        {
          path:'order',
          name:'order',
          component:Order,
          children: [
            {
              path: "comfirm",
              name: "comfirm",
              component: Comfirm
            }, {
              path: "checkOrder",
              name: "checkOrder",
              component: CheckOrder
            }, {
              path: "pay",
              name: "pay",
              component: Pay
            }, {
              path: "success",
              name: "success",
              component: Success
            }
          ]
        }
      ]
    },
    {
      path:"/login",
      name:"login",
      component:Login
    },
    {
      path:"/register",
      name:'register',
      component:Register
    },
    {
      path:'/*',
      name:'notfound',
      component:NotFound
    }
  ]
})

router.beforeEach((to,from,next)=>{

  let tokenIsExists=(localStorage.getItem('mytoken')?true:false)||(sessionStorage.getItem('mytoken')?true:false)
  if(to.path=='/index/basket'||to.path=='/index/about'){
    if(tokenIsExists){
      next()
    }else{
      next('/login')
    }
  }else{
    next()
  }
})
export default router;
